﻿// SurrogateWrapper.cs
//
// Copyright 2008 by Burt Harris
// Licensed under the Microsoft Public License (Ms-PL)
// See: http://www.codeplex.com/yaml/license
//
using System;
using System.Diagnostics;
using System.Runtime.Serialization;

namespace YaTools.Yaml.Serialization
{
    using YaTools.Yaml;

    [DebuggerDisplay("SurrogateWrapper ({_serializationSurrogate.GetType().Name})")]
    [Obsolete]
    class SurrogateWrapper : YamlSurrogate
    {
        static Uri s_baseUri = new Uri("tag:yatools.net,2009:ss,");

        ISerializationSurrogate _serializationSurrogate;

        public SurrogateWrapper( ISerializationSurrogate serializationSurrogate )
        {
            Utility.ValidateArgumentNotNull( serializationSurrogate, "serializationSurrogate" );
            this._serializationSurrogate = serializationSurrogate;
        }

        public override void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
        {
            Utility.ValidateArgumentNotNull(obj, "obj");
            var writer = context.YamlWriter();

            info = new SerializationInfo(obj.GetType(), null);
            _serializationSurrogate.GetObjectData(obj, info, context);

            if (writer.BeginMap( obj, GetUri(obj.GetType()), false))
            {
                try {
                    SerializationInfoEnumerator sie = info.GetEnumerator();
                    while (sie.MoveNext())
                    {
                        writer.WriteKey(sie.Name, Globals.TypeOfString);
                        writer.WriteItem(sie.Value, sie.ObjectType);
                    }
                }
                finally
                {
                    writer.EndMap();
                }
            }
        }

        public override object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
        {
            throw new NotImplementedException();
        }

        protected override Uri BaseUri
        {
            get { return s_baseUri; }
        }
    }
}
